const app = getApp()
let host = app.globalData.host;
Page({
  /**
   * 页面的初始数据
   */
  data: {
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    let deviceSerial = options.deviceSerial
    wx.request({
      url: host + '/showCamera?cameraId=1',
      method: "GET",
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
       },success (res) {
        let accessToken = res.data.cameraAccessToken
        wx.request({
          url: 'https://open.ys7.com/api/lapp/device/info',
          method: "POST",
          data: {
            accessToken: accessToken,
            deviceSerial: deviceSerial
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
           },success (res) {
             that.setData({
              status: res.data.data.status,
              deviceSerial: res.data.data.deviceSerial,
              deviceName: res.data.data.deviceName,
              model: res.data.data.model,
              category: res.data.data.category,
              netType: res.data.data.netType,
              signal: res.data.data.signal,
             })
             if (res.data.data.status == 1) {
               wx.request({
                 url: 'https://open.ys7.com/api/lapp/device/capture',
                 method: "POST",
                 data: {
                  accessToken: accessToken,
                  deviceSerial: deviceSerial,
                  channelNo: 1
                 },
                 header: {
                  'content-type': 'application/x-www-form-urlencoded' // 默认值
                 },success (res) {
                   console.log(res)
                   that.setData({
                     picUrl: res.data.data.picUrl
                   })
                 },fail (error){
                  console.log("失败")
               }
               })
             }
           },
        fail (error){
           console.log("失败")
        }
        })
       },
      fail (error){
       console.log("失败")
      }
    })
  },
  reSnap:function(e) {
    let that = this
    let deviceSerial = e.target.dataset.deviceserial
    wx.request({
      url: host + '/showCamera?cameraId=1',
      method: "GET",
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
       },success (res) {
        let accessToken = res.data.cameraAccessToken
        wx.request({
          url: 'https://open.ys7.com/api/lapp/device/info',
          method: "POST",
          data: {
            accessToken: accessToken,
            deviceSerial: deviceSerial
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
           },success (res) {
             if (res.data.data.status == 1) {
               wx.request({
                 url: 'https://open.ys7.com/api/lapp/device/capture',
                 method: "POST",
                 data: {
                  accessToken: accessToken,
                  deviceSerial: deviceSerial,
                  channelNo: 1
                 },
                 header: {
                  'content-type': 'application/x-www-form-urlencoded' // 默认值
                 },success (res) {
                   console.log(res)
                   that.setData({
                     picUrl: res.data.data.picUrl
                   })
                 },fail (error){
                  console.log("失败")
               }
               })
             }
           },
        fail (error){
           console.log("失败")
        }
        })
       },
      fail (error){
       console.log("失败")
      }
    })
  }
})